package com.dtw.ditouwang.ui.mine;

import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.dtw.ditouwang.R;
import com.dtw.ditouwang.beans.MoreBean;
import com.dtw.ditouwang.beans.UserSendCollectBean;
import com.dtw.ditouwang.databinding.ItemEditorChoosePhotoBinding;
import com.dtw.ditouwang.models.WebApi;
import com.dtw.ditouwang.ui.OnItemClickListener;
import com.dtw.ditouwang.ui.home.ImagePeopleAdapter;
import com.dtw.ditouwang.views.GlideRoundTransform;

import java.util.List;

public class MyCollectionAdapter extends RecyclerView.Adapter {
    private final int TypeImage=1,TypeSys=2;
    private List<UserSendCollectBean.DataBean> userSendCollectBeanList;
    RequestOptions requestOptions = RequestOptions.bitmapTransform(new GlideRoundTransform(10));
    private OnItemClickListener onItemClickListener;

    public MyCollectionAdapter(List<UserSendCollectBean.DataBean> userSendCollectBeanList) {
        this.userSendCollectBeanList = userSendCollectBeanList;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {

        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getItemViewType(int position) {
        if("2".equals(userSendCollectBeanList.get(position).getType())){
            return TypeSys;
        }else{
            return TypeImage;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if(i==TypeImage){
            ItemEditorChoosePhotoBinding binding = DataBindingUtil.inflate(LayoutInflater
                .from(viewGroup.getContext()), R.layout.item_editor_choose_photo, viewGroup, false);
            return new HolderPhoto(binding);
        }else {
            return new HolderSys(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_editor_choose_normal, viewGroup, false));
        }
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int i) {
        if(holder instanceof HolderPhoto){
            final HolderPhoto holderPhoto= (HolderPhoto) holder;
            MoreBean.DataBean dataBean=new MoreBean.DataBean();
            dataBean.setType("1");
            dataBean.setHead(userSendCollectBeanList.get(i).getHead());
            dataBean.setNickname(userSendCollectBeanList.get(i).getNickname());
            dataBean.setA_info(userSendCollectBeanList.get(i).getA_info());
            dataBean.setComments(userSendCollectBeanList.get(i).getComments());
            dataBean.setComments(userSendCollectBeanList.get(i).getCollection());
            dataBean.setImg(userSendCollectBeanList.get(i).getImg());
            holderPhoto.bindto(dataBean);
            holderPhoto.recyclerView.setLayoutManager(new GridLayoutManager(holder.itemView.getContext(),3));
            ImagePeopleAdapter imagePeopleAdapter=new ImagePeopleAdapter(dataBean.getImg());
            holderPhoto.recyclerView.setAdapter(imagePeopleAdapter);
            imagePeopleAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(int viewID,int position,int state) {
                    if (onItemClickListener!=null) {
                        onItemClickListener.onItemClick(viewID, holderPhoto.getAdapterPosition(), 0);
                    }
                }
            });
        }else{
            HolderSys holderSys= (HolderSys) holder;
            if(userSendCollectBeanList.get(i).getCover()!=null) {
                Glide.with(holderSys.imageView).load(WebApi.BaseUrl + userSendCollectBeanList.get(i).getCover().replace("\\", "/")).apply(requestOptions).into(holderSys.imageView);
            }else{
                holderSys.imageView.setImageBitmap(null);
            }
            holderSys.content.setText(userSendCollectBeanList.get(i).getTitle());
            holderSys.comment.setText(userSendCollectBeanList.get(i).getComments() + "");
            holderSys.like.setText(userSendCollectBeanList.get(i).getSee() + "");
        }
    }

    @Override
    public int getItemCount() {
        return userSendCollectBeanList.size();
    }

    class HolderPhoto extends RecyclerView.ViewHolder{
        private ItemEditorChoosePhotoBinding viewDataBinding;
        RecyclerView recyclerView;
        public HolderPhoto(ItemEditorChoosePhotoBinding binding) {
            super(binding.getRoot());
            this.viewDataBinding=binding;
            recyclerView = itemView.findViewById(R.id.recyclerview);
        }

        void bindto(MoreBean.DataBean dataBean){
            viewDataBinding.setPhoto(dataBean);
            viewDataBinding.executePendingBindings();
        }
    }

    class HolderSys extends RecyclerView.ViewHolder{
        ImageView imageView;
        TextView content, comment, like;
        public HolderSys(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.image_image);
            content = itemView.findViewById(R.id.text_content);
            comment = itemView.findViewById(R.id.text_comment);
            like = itemView.findViewById(R.id.text_like);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(0, getAdapterPosition(), 0);
                    }
                }
            });
        }
    }
}
